//
//  Problem1556.swift
//  TestProject
//
//  Created by 毕武侠 on 2021/3/21.
//  Copyright © 2021 zhulong. All rights reserved.
//

import UIKit

/*
 1556. 千位分隔数
 给你一个整数 n，请你每隔三位添加点（即 "." 符号）作为千位分隔符，并将结果以字符串格式返回。

 示例 1：
     输入：n = 987
     输出："987"
 示例 2：
     输入：n = 1234
     输出："1.234"
 示例 3：
     输入：n = 123456789
     输出："123.456.789"
 示例 4：
     输入：n = 0
     输出："0"
 提示：
    0 <= n < 2^31
 */
@objcMembers class Problem1556: NSObject {
    func solution() {
        print(thousandSeparator(1))
        print(thousandSeparator(12))
        print(thousandSeparator(987))
        print(thousandSeparator(1234))
        print(thousandSeparator(123456789))
    }
    
    func thousandSeparator(_ n: Int) -> String {
        if n == 0 {
            return "0"
        }
        var isThree = 0
        var num = Int(n / 10)
        var yu = n % 10
        var charts:[Character] = []
        while num != 0 || yu > 0 {
            let char = Character(Unicode.Scalar(yu + 48)!)
            
            if isThree == 3 {
                charts.insert(".", at: 0)
                isThree = 0
            }
            charts.insert(char, at: 0)
            isThree += 1
            
            yu = num % 10
            num = num / 10
        }
        return String(charts)
    }
    
}
